package com.jt.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.*;

import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 *
 * </p>
 *
 * @author Song
 * @since 2020/11/12
 */
@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {

    @Value("${redis.host}")
    private String host;

    @Value("${redis.port}")
    private Integer port;

    @Value("${redis.nodes}")
    private String nodes;

    @Value("${redis.sentinel}")
    private String sentinel;

    @Value("${redis.clusters}")
    private String clusters;

   /* @Bean
    public Jedis jedis(){
        return new Jedis(host,port);
    }*/

    /*@Bean
    public ShardedJedis jedis(){
        List<JedisShardInfo> shards = new ArrayList<>();
        String[] nodeArray= nodes.trim().split(",");
        for (String strNode : nodeArray){
            String host = strNode.split(":")[0];
            int port = Integer.parseInt(strNode.split(":")[1]);
            JedisShardInfo info = new JedisShardInfo(host, port);
            shards.add(info);
        }
        return new ShardedJedis(shards);
    }*/

    @Bean
    public JedisCluster jedis(){
        Set<HostAndPort> nodes = new HashSet<>();
        String[] nodeArray= clusters.trim().split(",");
        for (String strNode : nodeArray){
            String host = strNode.split(":")[0];
            int port = Integer.parseInt(strNode.split(":")[1]);
            nodes.add(new HostAndPort(host,port));
        }
        return new JedisCluster(nodes);
    }

}
